package com.efeichong.common.config;

import com.efeichong.enums.ResultEnum;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.ImmutableList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.security.Principal;
import java.util.List;

/**
 * @author lxk
 * @date 2020/9/2
 * @description knife4j/swagger 配置
 */
@ConditionalOnProperty(value = {"knife4j.enable"}, matchIfMissing = true)
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Value("${token.header}")
    private String tokenHeader;

    @Bean
    public Docket createRestApi(OpenApiExtensionResolver openApiExtensionResolver) {
        //定义统一状态码
        List<ResponseMessage> responses = ImmutableList.of(new ResponseMessageBuilder().code(ResultEnum.LOGIN_EXPIRED.getStatus())
                        .message(ResultEnum.LOGIN_EXPIRED.getMessage()).build(),
                new ResponseMessageBuilder().code(ResultEnum.FAILED.getStatus()).message(ResultEnum.FAILED.getMessage()).build(),
                new ResponseMessageBuilder().code(ResultEnum.SUCCESS.getStatus()).message(ResultEnum.SUCCESS.getMessage()).build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("efeichong"))
                //登录用户
                .ignoredParameterTypes(Principal.class)
                .globalResponseMessage(RequestMethod.GET, responses)
                .globalResponseMessage(RequestMethod.POST, responses)
                .globalResponseMessage(RequestMethod.PUT, responses)
                .globalResponseMessage(RequestMethod.DELETE, responses)
                .useDefaultResponseMessages(false)
                .securitySchemes(security());
    }

    /**
     * 文档说明
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("E飞虫")
                .description("接口文档")
                .version("1.0")
                .build();
    }

    /**
     * 请求头
     *
     * @return
     */
    private List<SecurityScheme> security() {
        List<SecurityScheme> apiKeys = ImmutableList.of(new ApiKey(tokenHeader, tokenHeader, "header"));
        return apiKeys;
    }

}